-- Copyright (c) 2024 Huawei Technologies Co., Ltd.
-- openUBMC is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at: http://license.coscl.org.cn/MulanPSL2
--
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.

local vos = require 'utils.vos'

local port_config = {}

-- 处理器资源树接口
local JAR_FILE_PATH <const> = "/opt/bmc/web/htdocs/bmc/pages/jar/vconsole.jar"
local KVMIP_PATH_PATTERN = "/bmc/kepler/Managers/1/NetworkProtocol/KVMIP/"
local VIRTUAL_MEDIA_PATH_PATTERN = "/bmc/kepler/Managers/1/NetworkProtocol/VirtualMedia/"
local PORT_CONFIG_INTERFACE = "bmc.kepler.Managers.NetworkProtocol.PortConfig"

---是否显示Java KVM入口
---@return boolean
function port_config.check_jar_file_exist()
    return vos.get_file_accessible(JAR_FILE_PATH)
end

function port_config.get_graphical_console(system_ids)
    local res = cjson.json_object_new_array()
    if not system_ids or system_ids == cjson.null or #system_ids == 0 then
        return res
    end
    local kvmip_obj
    for _, system_id in ipairs(system_ids) do
        kvmip_obj = mdb.get_object(bus, KVMIP_PATH_PATTERN .. system_id, PORT_CONFIG_INTERFACE)
        if not kvmip_obj then
            goto continue
        end
        local data = cjson.json_object_new_object()
        data['SystemId'] = system_id
        data['Port'] = kvmip_obj.Port
        data['Enabled'] = kvmip_obj.Enabled
        res[#res+1] = data
        ::continue::
    end
    return res
end

function port_config.parse_graphical_console(data)
    local res = cjson.json_object_new_object()
    if not data or data == cjson.null or #data == 0 then
        return res
    end
    for _, sub_data in ipairs(data) do
        local tmp_data = cjson.json_object_new_object()
        tmp_data['Port'] = sub_data.Port
        tmp_data['Enabled'] = sub_data.Enabled
        res[sub_data.SystemId] = tmp_data
    end
    return res
end

function port_config.get_virtual_media_config(system_ids)
    local res = cjson.json_object_new_array()
    if not system_ids or system_ids == cjson.null or #system_ids == 0 then
        return res
    end
    local vmm_obj
    for _, system_id in ipairs(system_ids) do
        vmm_obj = mdb.get_object(bus, VIRTUAL_MEDIA_PATH_PATTERN .. system_id, PORT_CONFIG_INTERFACE)
        if not vmm_obj then
            goto continue
        end
        local data = cjson.json_object_new_object()
        data['SystemId'] = system_id
        data['Port'] = vmm_obj.Port
        data['Enabled'] = vmm_obj.Enabled
        res[#res+1] = data
        ::continue::
    end
    return res
end

function port_config.parse_virtual_media_config(data)
    local res = cjson.json_object_new_object()
    if not data or data == cjson.null or #data == 0 then
        return res
    end
    for _, sub_data in ipairs(data) do
        local tmp_data = cjson.json_object_new_object()
        tmp_data['Port'] = sub_data.Port
        tmp_data['Enabled'] = sub_data.Enabled
        res[sub_data.SystemId] = tmp_data
    end
    return res
end

return port_config
